package com.formiaomiao.demain.service.impl;

import com.formiaomiao.demain.dao.*;
import com.formiaomiao.demain.model.*;
import com.formiaomiao.demain.service.DemainService;
import com.formiaomiao.demain.utils.QiniuUtil;
import com.formiaomiao.demain.utils.ResultUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(value = "demainService")
public class DemainServiceImpl implements DemainService {

    @Autowired
    private DemainDao demainDao;//这里会报错，但是并不会影响
    @Autowired
    private ImgDao imgDao;
    @Autowired
    private LikeDao likeDao;
    @Autowired
    private CommentDao commentDao;
    @Autowired
    FriendRemarksDao friendRemarksDao;

    QiniuUtil qiniuUtil = new QiniuUtil();

    @Override
    public Result<DemainBean> SelectDemain(Integer id, Integer fromid, int pageNum, int pageSize) {
        if (demainDao.SelectDemain(id).size() == 0) {
            return ResultUtil.error(400, "暂没有动态信息");
        } else if (demainDao.SelectDemain(id).size() > 0) {
            PageHelper.startPage(pageNum, pageSize);
            List<DemainBean> list = demainDao.SelectDemain(id);
            for (int i = 0; i < list.size(); i++) {
                FriendRemarksBean friendRemarksBean = new FriendRemarksBean();
                friendRemarksBean.setFriendfrom(fromid);
                friendRemarksBean.setFriendto(id);
                list.get(i).setImgBean(imgDao.imgidlist(list.get(i).getDataid()));
                if (friendRemarksDao.SelectRemarks(friendRemarksBean) == null) {
                    list.get(i).setFriendremarks("");
                } else {
                    list.get(i).setFriendremarks(friendRemarksDao.SelectRemarks(friendRemarksBean)
                            .getFriendremarks() == null ? "" : friendRemarksDao.SelectRemarks(friendRemarksBean)
                            .getFriendremarks());
                }
                int likecount = 0;
                for (int j = 0; j < likeDao.selectLike(list.get(i).getDataid()).size(); j++) {
                    if (likeDao.selectLike(list.get(i).getDataid()).get(j).getLiketype() == 1) {
                        likecount += 1;
                    }
                }
                list.get(i).setLikecount(likecount);
                int msgcount = 0;
                for (int j = 0; j < commentDao.SelectComment(list.get(i).getDataid()).size(); j++) {
                    msgcount += 1;
                }
                list.get(i).setMsgcount(msgcount);
                if (likeDao.checkLike(list.get(i).getDataid(), fromid) == null) {
                    list.get(i).setIslike(0);
                } else {
                    list.get(i).setIslike(likeDao.checkLike(list.get(i).getDataid(), fromid).getLiketype());
                }
            }
            ResultData result = new ResultData(pageNum, pageSize, list);
            return ResultUtil.success(result);
        } else {
            return ResultUtil.error(400, "获取失败");
        }
    }

    @Transactional(value = "demain")
    @Override
    public Result<String> InsertDemain(DemainBean bean) {
        if (demainDao.InsertDemain(bean) == 0) {
            return ResultUtil.error(400, "添加失败");
        } else {
            if (bean.getImgBean() != null) {
                for (int i = 0; i < bean.getImgBean().size(); i++) {
                    if (imgDao.InsertDemainImg(bean.getImgBean().get(i)) == 0) {
                        return ResultUtil.error(400, "添加失败");
                    }
                }
            }
            return ResultUtil.success("发布成功");
        }
    }

    @Transactional(value = "demain")
    @Override
    public Result<String> DeleteDemain(DemainBean bean) {
        if (demainDao.DeleteDemain(bean) == 0) {
            return ResultUtil.error(400, "删除失败");
        } else {
            if (imgDao.imgidlist(bean.getDataid()).size() > 0) {
                for (int i = 0; i < imgDao.imgidlist(bean.getDataid()).size(); i++) {
                    try {
                        qiniuUtil.deleteImg(imgDao.imgidlist(bean.getDataid()).get(i).getImgkey());
                    } catch (QiniuException e) {
                        e.printStackTrace();
                    }
                }
                CommentBean commentBean = new CommentBean();
                commentBean.setComid(bean.getDataid());
                LikeBean likeBean = new LikeBean();
                likeBean.setLikeid(bean.getDataid());
                if (imgDao.DeleteDemainImg(bean.getDataid()) == 0) {
                    return ResultUtil.error(400, "删除失败");
                } else if (commentDao.DeleteComment(commentBean) == 0) {
                    return ResultUtil.error(400, "删除失败");
                } else if (likeDao.deleteLike(likeBean) == 0) {
                    return ResultUtil.error(400, "删除失败");
                } else {
                    return ResultUtil.success("删除成功");
                }
            } else {
                return ResultUtil.success("删除成功");
            }
        }
    }

    @Override
    public Result<DemainBean> SelectAllDemain(Integer id, int pageNum, int pageSize) {
        if (demainDao.SelectAllDemain(id).size() == 0) {
            return ResultUtil.error(400, "暂没有动态信息");
        } else if (demainDao.SelectAllDemain(id).size() > 0) {
            PageHelper.startPage(pageNum, pageSize);
            List<DemainBean> list = demainDao.SelectAllDemain(id);
            for (int i = 0; i < list.size(); i++) {
                FriendRemarksBean friendRemarksBean = new FriendRemarksBean();
                friendRemarksBean.setFriendfrom(id);
                friendRemarksBean.setFriendto(list.get(i).getIduser());
                list.get(i).setImgBean(imgDao.imgidlist(list.get(i).getDataid()));
                if (friendRemarksDao.SelectRemarks(friendRemarksBean) == null) {
                    list.get(i).setFriendremarks("");
                } else {
                    list.get(i).setFriendremarks(friendRemarksDao.SelectRemarks(friendRemarksBean)
                            .getFriendremarks() == null ? "" : friendRemarksDao.SelectRemarks(friendRemarksBean)
                            .getFriendremarks());
                }
                int likecount = 0;
                for (int j = 0; j < likeDao.selectLike(list.get(i).getDataid()).size(); j++) {
                    if (likeDao.selectLike(list.get(i).getDataid()).get(j).getLiketype() == 1) {
                        likecount += 1;
                    }
                }
                list.get(i).setLikecount(likecount);
                int msgcount = 0;
                for (int j = 0; j < commentDao.SelectComment(list.get(i).getDataid()).size(); j++) {
                    msgcount += 1;
                }
                list.get(i).setMsgcount(msgcount);
                if (likeDao.checkLike(list.get(i).getDataid(), id) == null) {
                    list.get(i).setIslike(0);
                } else {
                    list.get(i).setIslike(likeDao.checkLike(list.get(i).getDataid(), id).getLiketype());
                }
            }
            ResultData result = new ResultData(pageNum, pageSize, list);
            return ResultUtil.success(result);
        } else {
            return ResultUtil.error(400, "获取失败");
        }
    }
}
